<?php
  require_once("../includes/config.php");
  
    $length = 20;
    
    if( isset($_POST['page'])  && !empty($_POST['page'] ))
    {
      $page =  filter_var($_POST['page'], FILTER_SANITIZE_NUMBER_INT);
      $start = $page*20 - 20;
      unset($_POST['page']);
    }
    else
    {
      $start = 0;
      $page = 1;
    }
    
    $sql_p = "SELECT * FROM payment_vouchers
            	LEFT JOIN contracts ON 
				payment_vouchers.contract_id=contracts.contract_id 
				WHERE payment_vouchers.owner_id= ? and payment_vouchers.payment_type = ? ";
    $sql_r = "SELECT * FROM recipt_vouchers
            	LEFT JOIN contracts ON
				recipt_vouchers.contract_id=contracts.contract_id
				WHERE recipt_vouchers.owner_id= ? and recipt_vouchers.payment_type = ? ";
   $sum_p= "SELECT SUM(amount) FROM payment_vouchers WHERE owner_id = ? and payment_type = ?";
   $sum_r= "SELECT SUM(amount) FROM recipt_vouchers WHERE owner_id = ? and payment_type = ?";
    
    $binds['owner_id'] = $_SESSION['user_id'];
	 $binds['payment_type'] = 0 ;
  
    if(!empty($_POST['from']) && !empty($_POST['to']))
    {
      $binds['from'] = $_POST['from'];
      $binds['to'] = $_POST['to'];
      $sql_p .= " and miladi_date between ? AND ?";
      $sql_r .= " and miladi_date between ? AND ?";
      $sum_p .= " and miladi_date between ? AND ?";
      $sum_r .= " and miladi_date between ? AND ?";
    }
    $payments_counter = query($sql_p,$binds);
    $reciepts_counter = query($sql_r,$binds);
    if(  count($payments_counter) > count($reciepts_counter) )
      $totalPages =  count($payments_counter);
    else
      $totalPages =  count($reciepts_counter);
      
    $sql_p .= " LIMIT ?,?";                   
    $sql_r .= " LIMIT ?,?";
	
    $binds['start'] = $start;
    $binds['end'] = $length;
    
    $payments = query($sql_p,$binds);
	$reciepts = query($sql_r,$binds);
    $sum_payments=query($sum_p,$binds);
	$sum_reciepts=query($sum_r,$binds);
	if(isset($sum_payments[0]['SUM(amount)']))
		$total_sum_p= $sum_payments[0]['SUM(amount)'];
	else
		$total_sum_p = 0;
		
	if(isset($sum_reciepts[0]['SUM(amount)']))
		$total_sum_r = $sum_reciepts[0]['SUM(amount)'];
	else
		$total_sum_r = 0;
	$i = 0;
	$box = array();
	$boxes = array();

	while(1)
	{
    if(isset($payments[$i]) || isset($reciepts[$i]))
		{
			if(isset($payments[$i]))
				$payment = $payments[$i];
			else
      {
				$payment = array();
				$reciept['contract_id_r'] = "";
        $reciept['recipet_id'] = "";
				$reciept['amount']= "";
				$reciept['reason']= "";
				$reciept['miladi_date']= "";
      }
			
			if(isset($reciepts[$i]))
				$reciept = $reciepts[$i];
				
			else
			{
				$reciept = array();
				$reciept['contract_id_r'] = "";
				$reciept['recipet_id'] = "";
				$reciept['amount']= "";
				$reciept['reason']= "";
				$reciept['miladi_date']= "";
			}
			
			$box = $payment;
			$box['contract_id_r'] = $reciept['contract_id'];
			$box['recipet_id'] = $reciept['recipet_id'];
			$box['reciept_amount'] = $reciept['amount'];
			$box['reciept_reason'] = $reciept['reason'];
			$box['reciept_miladi_date'] = $reciept['miladi_date'];
      $boxes[] = $box;
      $i++;
    }
		else
			break;
	}
  
  
    render("box_report.php", array("title"=>"تقرير حركة الصندوق","page"=>$page,"boxes"=>$boxes,"start"=>$start,"totalPages"=>$totalPages,"length"=>$length,"binds"=>$binds,"total_sum_p"=>$total_sum_p,"total_sum_r"=>$total_sum_r));

?>